package com.usermanage.dataDao;

import com.usermanage.beanParam.ContractBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 過客 on 2018/8/30
 */
@Slf4j
@Repository("jpaDao")
//public class JpaDaoImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
//        implements JpaDao<T, ID> {
    public class JpaDaoImpl<T> implements JpaDao<T> {
    //用于操作数据库
    @PersistenceContext(name="entityManagerEos",unitName="eosPersistenceUnit")
    private  EntityManager em;
//    private final Class<T> tClass;
//    public JpaDaoImpl(Class<T> domainClass, EntityManager entityManager) {
//        super(domainClass, entityManager);
//        this.em = entityManager;
//        this.tClass = domainClass;
//    }

    @Override
    @Transactional
    public List<T> getListByPage(String sql,Class<T> tClass) {
        List<T> result = new ArrayList<>();
        try {
            Query query=em.createNativeQuery(sql,tClass);
            result= query.getResultList();
        }catch (Exception e){

        }finally {
            em.close();
        }
        return result;
    }

    @Override
    @Transactional
    public int getListCountByPage(String sql) {
        int result = 0;
        try {
            Query query=em.createNativeQuery(sql);
            result= Integer.valueOf(query.getResultList().get(0).toString());
        }catch (Exception e){

        }finally {
            em.close();
        }
        return result;
    }
}
